package ar.edu.uba.fi.jmit.aspecter;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import ar.edu.uba.fi.jmit.aspecter.core.Constants;
import ar.edu.uba.fi.jmit.aspecter.core.exception.AspecterException;
import ar.edu.uba.fi.jmit.aspecter.testUtils.SpyAspecterMain;

public class AspecterMainTestCase extends AspecterTestCase {

	public void testInvalidArgs() {
		assertFalse(SpyAspecterMain.validateArgs(new String[]{}));
		assertFalse(SpyAspecterMain.validateArgs(new String[]{""}));
		assertFalse(SpyAspecterMain.validateArgs(new String[]{"", ""}));
		assertFalse(SpyAspecterMain.validateArgs(new String[]{"./myXml.jmit.xml", "target/test-classes/ar", "./target/test-classes"}));
		assertTrue(SpyAspecterMain.validateArgs(new String[]{"testApplication", "./myXml.jmit.xml", "target/test-classes/ar", "./target/test-classes"}));
		assertTrue(SpyAspecterMain.validateArgs(new String[]{"testApplication", "./myXml.jmit.xml", "target/test-classes/ar", "./target/test-classes", "development"}));
		assertTrue(SpyAspecterMain.validateArgs(new String[]{"testApplication", "./myXml.jmit.xml", "target/test-classes/ar", "./target/test-classes", "testing"}));
		assertTrue(SpyAspecterMain.validateArgs(new String[]{"testApplication", "./myXml.jmit.xml", "target/test-classes/ar", "./target/test-classes", "production"}));
		assertFalse(SpyAspecterMain.validateArgs(new String[]{"testApplication", "./myXml.jmit.xml", "target/test-classes/ar", "./target/test-classes", "someOtherThing"}));
		assertFalse(SpyAspecterMain.validateArgs(new String[]{"testApplication", "./myXml.jmit.xml", "target/test-classes/ar", "./target/test-classes", "development", ""}));
	}
	
	public void testCreatedFile() {
		SpyAspecterMain aspecter = new SpyAspecterMain("testApplication", "./myXml.jmit.xml", "target/test-classes/ar", "./target/test-classes", "development");
		Map<String, String> monitorEngineProps = new HashMap<String, String>();
		try {
			aspecter.configureSender(monitorEngineProps);
			fail("exception should be thrown");
		} catch (AspecterException e) {
			//Expected
		}
		monitorEngineProps.put("protocol", "http");
		try {
			aspecter.configureSender(monitorEngineProps);
			fail("exception should be thrown");
		} catch (AspecterException e) {
			//Expected
		}
		monitorEngineProps.put("ip", "127.0.0.1");
		try {
			aspecter.configureSender(monitorEngineProps);
			fail("exception should be thrown");
		} catch (AspecterException e) {
			//Expected
		}
		monitorEngineProps.put("port", "8080");
		try {
			aspecter.configureSender(monitorEngineProps);
			fail("exception should be thrown");
		} catch (AspecterException e) {
			//Expected
		}
		monitorEngineProps.put("context", "receiverMock");
		try {
			aspecter.configureSender(monitorEngineProps);
			InputStream senderConfigFile = this.getClass().getClassLoader().getResourceAsStream(Constants.SENDER_CONFIGURATION_FILE_NAME);
			assertNotNull("Sender configuration file not found", senderConfigFile);
			BufferedInputStream bis = (BufferedInputStream) senderConfigFile;
			byte[] buff = new byte[256];
			bis.read(buff);
			String configuration = new String(buff);
			assertEquals("http", this.getProperty(configuration, "protocol"));
			assertEquals("127.0.0.1", this.getProperty(configuration, "ip"));
			assertEquals("8080", this.getProperty(configuration, "port"));
			assertEquals("receiverMock", this.getProperty(configuration, "context"));
			assertEquals(Constants.MONITOR_ENGINE_DESTINATION_NAME, this.getProperty(configuration, "destinationName"));
			assertEquals("testApplication", this.getProperty(configuration, "applicationSource"));
			
		} catch (Exception e) {
			fail(e);
		} 
	}

	/**
	 * Devuelve un atributo de configuracion del archvo de propiedades del sender
	 * @param configuration
	 * @param string
	 * @return
	 */
	private String getProperty(String configuration, String property) {
		int from = configuration.indexOf(property) + property.length() + 1;
		int to = configuration.indexOf("\n", from);
		return configuration.substring(from, to);
	}
}
